package com.weiyuwqj.admin.controller.common;

import com.weiyuwqj.api.tool.QiniuUpload;
import com.weiyuwqj.api.util.JsonUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Copyright 2015-2019 Yunfucloud technology co., Ltd.
 * Created by fuxx on 16/4/5.
 */
public class UeditorUpload {

    public static String handlerUeditor(HttpServletRequest request,String json){
        Map<String,Object> ret= JsonUtils.json2Map(json);

        String webRootPath=request.getServletContext().getRealPath("/");

        String fullPath=handleString(ret.get("url"));
        String newFileName=handleString(ret.get("title"));
        QiniuUpload upload=new QiniuUpload();
        String url=upload.uploadQiniu(webRootPath+fullPath,newFileName);
        ret.put("url",url);
        return JsonUtils.toJson(ret);
    }

    public static String handleString(Object obj){
        if(obj==null){
            return "";
        }else{
            return  obj.toString();
        }
    }

}
